-
Notifications
You must be signed in to change notification settings - Fork 967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve State
and Router
docs
#1543
Improve State
and Router
docs
#1543
Conversation
Per my comment in tokio-rs#1313, this improves discoverability of how `State` works with nested/merged `Router`s. I made a few other minor documentation fixes while I was reading: * Corrected the docs for `Router::nest` to say that it nests another `Router` rather than a `Service`. There is a separate function (`Router::nest_service`) for the latter. * Changed one of the headings in nest.md to use more idiomatic English. * Changed awkward phrasing under the "Sharing state with handlers" heading of the root documentation page. * Removed a trailing period from one of three list items for consistency.
Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Before merging this I want to make sure what I wrote is actually correct and clear. After reading #1532 more closely I noticed:
After experimenting with nested routers, that statement seems more accurate, and this current sentence from the docs of
This doesn't really make sense, because you can't construct a Am I understanding this correctly? If so, I'll need to revise the sentence I added and would also like to change the sentence from |
In fact, it seems that once you have a For example: #[derive(Clone)]
struct AppState {
count: u64,
}
pub fn api() -> RouterService {
Router::<AppState>::new()
.nest("/api/v1", api_v1())
// ^^^^^^^^ expected struct `AppState`, found `()`
.fallback(fallback)
.with_state(AppState { count: 0 })
}
fn api_v1() -> Router {
Router::new()
.route("/teams", get(get_teams))
.route("/teams/:slug", get(get_team))
} How can you possibly use |
Sure you can. Just do: fn api_v1() -> Router<AppState> {
Router::new()
.route("/teams", get(get_teams))
.route("/teams/:slug", get(get_team))
} |
Nesting routers with different state is possible by calling |
Ah, I see. So the state type of a router is inferred if I'm going to revise my changes tomorrow now that I understand it better. I suspect I won't be the last person to be confused by this without better docs. Thanks for the example! |
Yes exactly.
Thanks! |
State
and Router
docs
Okay, I pushed up another commit which expands the docs on combining stateful routers. I added a section to the |
By the way you might be interested in #1552 |
Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
Per my comment in #1313, this improves discoverability of how
State
works with nested/mergedRouter
s.I made a few other minor documentation fixes while I was reading:
Router::nest
to say that it nests anotherRouter
rather than aService
. There is a separate function (Router::nest_service
) for the latter.